package com.carpedil.smvc.dao;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carpedil.smvc.po.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/** @author Administrator */
@Repository("userMapper")
public interface UserMapper extends BaseMapper<User> {
  /**
   * getUsersByPageParam
   *
   * @param page page
   * @param wrapper wrapper
   * @return IPage<User>
   */
  @Select(
      "select * "
          + " from user_role ur "
          + " inner join users u on u.id = ur.fk_user_id "
          + " inner join roles r on r.pk_id = ur.fk_role_id ${ew.customSqlSegment}")
  @ResultMap("com.carpedil.smvc.dao.UserMapper.userMap")
  IPage<User> getUsersByPageParam(Page<User> page, @Param(Constants.WRAPPER) Wrapper<User> wrapper);

  /**
   * getAllUsers
   *
   * @return Users
   */
  @Select(
      "select user_name, nick_name,pass_word,user_address,user_tel,user_img,role_name,reg_time,permission_pk_id,permission_name,pid "
          + " from user_role ur"
          + " inner join users u on u.id = ur.fk_user_id"
          + " inner join roles r on r.pk_id = ur.fk_role_id"
          + " inner join user_detail ud on u.fk_user_detail_id = ud.user_detail_id"
          + " inner join role_permission rp on r.pk_id = rp.fk_role_id"
          + " inner join permissions p on rp.fk_permission_id = p.permission_pk_id"
          + " ${ew.customSqlSegment}")
  @ResultMap("com.carpedil.smvc.dao.UserMapper.userMap")
  List<User> getUsersByWrapper(@Param(Constants.WRAPPER) Wrapper<User> wrapper);

  @Select(
      "select u.id, user_name, nick_name,pass_word,user_address,user_img,user_tel,role_name,reg_time,permission_pk_id,permission_name,pid "
          + " from user_role ur"
          + " inner join users u on u.id = ur.fk_user_id"
          + " inner join roles r on r.pk_id = ur.fk_role_id"
          + " inner join user_detail ud on u.fk_user_detail_id = ud.user_detail_id"
          + " inner join role_permission rp on r.pk_id = rp.fk_role_id"
          + " inner join permissions p on rp.fk_permission_id = p.permission_pk_id"
          + " ${ew.customSqlSegment}")
  @ResultMap("com.carpedil.smvc.dao.UserMapper.userMap")
  User login(@Param(Constants.WRAPPER) Wrapper<User> wrapper);

  /**
   * login2
   *
   * @param userName userName
   * @param passWord passWord
   * @return User
   */
  @Select("select * " + " from users" + " where user_name =#{userName} and pass_word=#{passWord}")
  @ResultMap("com.carpedil.smvc.dao.UserMapper.userMap2")
  User login2(@Param("userName") String userName, @Param("passWord") String passWord);
}
